package socialplaytime;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/** This class builds the Social Playtime main user interface including a frame, panels, labels and buttons.
 * 
 * @author Heather Blanchard
 * @author Kyle Damarin
 * @author Euna Jang
 * @author Shannon Fluellen
 * @version 1.0 Build 213 August 17, 2012
 *
 */
public class PlayMenuGUI implements ActionListener, SocialPlaytimeConstants {

	/** 
	 * The attributes mod1, mod2, mod3, and mod4 represent the four different modules that can be displayed.
	 */
	private int mod1=1, mod2=2, mod3=3, mod4=4;
	/**
	 * The modulenum array represents which modules will be available for a particular student.
	 */
	private char[]  modulenum = new char[4];
	/**
	 * The JFrame frame displays the welcome message and module buttons.
	 */
	private JFrame frame;
	/**
	 * The StoryWindow storyframe is a new frame that is called by clicking a module button.
	 */
	@SuppressWarnings("unused")
	private StoryWindow storyFrame;
	/**
	 * The JLabels welcome, choose, and blank greets the user by name, prompts the user
	 * to choose a module, and is a place holder for an empty label respectively. 
	 */
	private JLabel welcome, choose, blank;
	/**
	 * The JButtons m1button, m2button, m3button, and m4button represent each of the four modules.
	 */
	private JButton m1Button, m2Button, m3Button, m4Button;

	/**
	 * @return PlayMenuGUI frame
	 */
	public JFrame getFrame() {
		return frame;
	}

	/**
	 * PlayMenuGUI() creates a PlayMenuGUI object.
	 * @param studentName 
	 * @param playSet 
	 */
	public PlayMenuGUI(String studentName, PlaySet playSet) {
		//ps is a Menu2 object that reads the student names from a text file.
		if (studentName != null) {
			//modulenum is an array of chars filled by the PlaySet getModulenum(String) method.
			modulenum = playSet.getModulenum(studentName);
			//the number 9 in the modulenum array represents an administrator and calls a new PlaySetDataFrame
			if (modulenum[0] == '9') {
				//PlaySetDataFrame object is created
				PlaySetDataFrame ps1 = new PlaySetDataFrame(playSet, this);
				//PlaySetDataFrame is made visible
				ps1.setVisible(true);
				//size of the new PlaySetDataFrame
				ps1.setSize(400, 400);
			} else {
				welcome = new JLabel(WELCOME_LABEL + studentName + "!");
				choose = new JLabel(CHOOSE_MODULE_LABEL);
				blank = new JLabel(BLANK_LABEL);
				m1Button = new JButton(MODULE_1_LABEL);
				m2Button = new JButton(MODULE_2_LABEL);
				m3Button = new JButton(MODULE_3_LABEL);
				m4Button = new JButton(MODULE_4_LABEL);

				//actionListeners for the module buttons
				m1Button.addActionListener(this);
				m2Button.addActionListener(this);
				m3Button.addActionListener(this);
				m4Button.addActionListener(this);

				//layout

				//Welcome message panel
				JPanel nWelcomeMessage = new JPanel(new FlowLayout());
				nWelcomeMessage.add(welcome);

				//Choose message panel
				JPanel nChooseMessage = new JPanel(new FlowLayout());
				nChooseMessage.add(choose);

				//North panel which holds one welcome panel, one choose panel, and one blank panel in a 3x1 grid
				JPanel north = new JPanel(new GridLayout(3,1));
				north.add(blank);
				north.add(nWelcomeMessage);
				north.add(nChooseMessage);

				//Modules panel which adds module buttons ion a 2x2 grid according to the data stored in the modulenum array.
				JPanel modules = new JPanel(new GridLayout(2,2));
				for (int i = 0; i < modulenum.length; i++) {
					if (modulenum[i] == '1') {
						modules.add(m1Button);
					} else if (modulenum[i] == '2') {
						modules.add(m2Button);
					} else if (modulenum[i] == '3') {
						modules.add(m3Button);
					} else if (modulenum[i] == '4') {
						modules.add(m4Button);					
					}
				}

				//overall frame
				frame = new JFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(new Dimension(800, 600));
				frame.setTitle("Social Playtime");
				frame.setLayout(new BorderLayout());
				frame.add(north, BorderLayout.NORTH);
				frame.add(modules, BorderLayout.CENTER);
				frame.setVisible(true);

			}
		}
	}

	public void actionPerformed(ActionEvent event) {
		if (event.getSource() instanceof JButton) {
			JButton button = (JButton)event.getSource();
			String label = button.getText();
			if (label.equals(MODULE_1_LABEL)) {
				storyFrame = new StoryWindow(mod1, this);
			}
			if (label.equals(MODULE_2_LABEL)) {
				storyFrame = new StoryWindow(mod2, this);
			}
			if (label.equals(MODULE_3_LABEL)) {
				storyFrame = new StoryWindow(mod3, this);
			}
			if (label.equals(MODULE_4_LABEL)) {
				storyFrame = new StoryWindow(mod4, this);
			}
			frame.setVisible(false);
		}
	}
}